//
//  HomePageDataHandle.m
//  LOLProject
//
//  Created by lanou3g on 15/5/29.
//  Copyright (c) 2015年 lanou3g.com蓝欧科技. All rights reserved.
//

#import "HomePageDataHandle.h"

@implementation HomePageDataHandle

static HomePageDataHandle * homePageDataHandle=nil;

//创建首页的单例对象
+ (HomePageDataHandle *) sharedInstance{
    @synchronized(self){
        if (homePageDataHandle==nil) {
            homePageDataHandle=[[HomePageDataHandle alloc] init];
        }
        return homePageDataHandle;
    }
}

//请求数据
- (void) requestHomePageData:(NSInteger) selectedSegmentIndex
                   DataArray:(NSMutableArray *)dataArray{
    if (selectedSegmentIndex==0) {
        [self requestHomePageDataTheLatestVideoWithDataArray:dataArray];
    }else if (selectedSegmentIndex==1){
        [self requestHomePageDataVideoGameWithDataArray:dataArray];
    }else if (selectedSegmentIndex==2){
        [self requestHomePageDataHdBroadcastWithDataArray:dataArray];
    }else{
        return ;
    }
    return ;
}

//从网上请求最新视频数据
- (void) requestHomePageDataTheLatestVideoWithDataArray:(NSMutableArray *)dataArray{
//    异步连接下的GET请求
//    1.创建网址对象
    NSString * string=[kTheLatestVideoData_List_URL stringByReplacingOccurrencesOfString:@"page=1" withString:[NSString stringWithFormat:@"page=%ld",dataArray.count/20+1]];
    NSURL * url=[NSURL URLWithString:string];
//    2.创建网络请求对象
    NSURLRequest * request=[NSURLRequest requestWithURL:url];
//    3.创建连接对象,并发送请求
//    用来存储封装好的数据
    NSMutableArray * array=[NSMutableArray arrayWithArray:dataArray];
    __weak HomePageDataHandle * homePageDataHandle=self;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//        代码保护
        if (data==nil) {
            return ;
        }
//        4.解析数据
        NSDictionary * sourceDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        处理数据(封装model对象)
        for (NSDictionary * dic in [sourceDic objectForKey:@"videos"]) {
            TheLatestVideoList * theLatestVideoList=[[TheLatestVideoList alloc] init];
            [theLatestVideoList setValuesForKeysWithDictionary:dic];
            [array addObject:theLatestVideoList];
        }
//        回调videoGameBlock
        homePageDataHandle.theLatestVideoBlock(array);
    }];
    return ;
}

//从网上请求比赛视频数据
- (void) requestHomePageDataVideoGameWithDataArray:(NSMutableArray *)dataArray{
//    异步连接下的GET请求
//    1.创建网址对象
    NSString * string=[kVideoGameData_List_URL stringByReplacingOccurrencesOfString:@"page=1" withString:[NSString stringWithFormat:@"page=%ld",dataArray.count/20+1]];
    NSURL * url=[NSURL URLWithString:string];
//    2.创建网络请求对象
    NSURLRequest * request=[NSURLRequest requestWithURL:url];
//    3.创建连接对象,并发送请求
//    用来存储封装好的数据
    NSMutableArray * array=[NSMutableArray array];
    __weak HomePageDataHandle * homePageDataHandle=self;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//        代码保护
        if (data==nil) {
            return ;
        }
//        4.解析数据
        NSDictionary * sourceDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        处理数据(封装model对象)
        for (NSDictionary * dic in [sourceDic objectForKey:@"playlists"]) {
            VideoGameList * videoGameList=[[VideoGameList alloc] init];
            [videoGameList setValuesForKeysWithDictionary:dic];
            [array addObject:videoGameList];
        }
//        回调videoGameBlock
        homePageDataHandle.videoGameBlock(array);
    }];
    return ;
}

//从网上请求高清视频数据
- (void) requestHomePageDataHdBroadcastWithDataArray:(NSMutableArray *)dataArray{
//    异步连接下的GET请求
//    1.创建网址对象
    NSString * string=[kHdBroadcastData_List_URL stringByReplacingOccurrencesOfString:@"offset=0" withString:[NSString stringWithFormat:@"offset=%ld",(dataArray.count/20+1)*20]];
    NSURL * url=[NSURL URLWithString:string];
//    2.创建网络请求对象
    NSURLRequest * request=[NSURLRequest requestWithURL:url];
//    3.创建连接对象,并发送请求
//    用来存储封装好的数据
    NSMutableArray * array=[NSMutableArray array];
    __weak HomePageDataHandle * homePageDataHandle=self;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//        代码保护
        if (data==nil) {
            return ;
        }
//        4.解析数据
        NSDictionary * sourceDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        处理数据(封装model对象)
        for (NSDictionary * dic in [sourceDic objectForKey:@"data"]) {
            HdBroadcastList * hdBroadcastList=[[HdBroadcastList alloc] init];
            [hdBroadcastList setValuesForKeysWithDictionary:dic];
            [array addObject:hdBroadcastList];
        }
//        回调videoGameBlock
        homePageDataHandle.hdBroadcastBlock(array);
    }];
    return ;
}

@end
